package com.atguigu.gmall.pms.service.impl;

import com.atguigu.core.bean.PageVo;
import com.atguigu.core.bean.Query;
import com.atguigu.core.bean.QueryCondition;
import com.atguigu.gmall.pms.dao.AttrAttrgroupRelationDao;
import com.atguigu.gmall.pms.dao.AttrDao;
import com.atguigu.gmall.pms.dao.AttrGroupDao;
import com.atguigu.gmall.pms.dao.ProductAttrValueDao;
import com.atguigu.gmall.pms.entity.AttrAttrgroupRelationEntity;
import com.atguigu.gmall.pms.entity.AttrEntity;
import com.atguigu.gmall.pms.entity.AttrGroupEntity;
import com.atguigu.gmall.pms.entity.ProductAttrValueEntity;
import com.atguigu.gmall.pms.service.AttrGroupService;
import com.atguigu.gmall.pms.vo.GroupVO;
import com.atguigu.gmall.pms.vo.ItemGroupVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {


    @Autowired
    private AttrAttrgroupRelationDao relationDao;

    @Autowired
    private AttrDao attrDao;

    @Autowired
    private ProductAttrValueDao attrValueDao;

    @Override
    public PageVo queryPage(QueryCondition params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageVo(page);
    }

    @Override
    public PageVo queryGroupByPage(QueryCondition condition, Long cid) {

        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<>();
        if (cid != null) {
            wrapper.eq("catelog_id", cid);
        }
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(condition),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageVo(page);
    }

    @Override
    public GroupVO queryGroupWithAttrByGid(Long gid) {

        GroupVO groupVo = new GroupVO();
        //查询group
        AttrGroupEntity attrGroupEntity = this.getById(gid);
        BeanUtils.copyProperties(attrGroupEntity, groupVo);
        //根据gid 查询关联关系,并获取attrIds
        List<AttrAttrgroupRelationEntity> relations = this.relationDao.selectList(
                new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_group_id", gid));
        if (CollectionUtils.isEmpty(relations)) {
            return groupVo;
        }
        groupVo.setRelations(relations);
        // 收集分组下的所有规格id
        List<Long> attrIds = relations.stream().map(relation -> relation.getAttrId()).collect(Collectors.toList());
        List<AttrEntity> entities = this.attrDao.selectBatchIds(attrIds);
        groupVo.setAttrEntities(entities);

        return groupVo;
    }

    @Override
    public List<GroupVO> queryGroupWithAttrsByCid(Long catId) {

        //根须cid 查询三级分类下的所有分组
        List<AttrGroupEntity> groupEntities = this.list(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catId));

        //1.根据分组中的id查询中间表
        //2.根据中间表attrIds查询参数
        //3.数据类型的转化 attrgroupEntity --> groupVO
        return groupEntities.stream().map(attrGroupEntity -> this.queryGroupWithAttrByGid(attrGroupEntity.getAttrGroupId()))
                .collect(Collectors.toList());

    }

    @Override
    public List<ItemGroupVO> queryItemGroupVOByCidAndSpuId(Long cid, Long spuId) {

        List<AttrGroupEntity> attrGroupEntities = this.list(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", cid));

        return attrGroupEntities.stream().map(group -> {
            ItemGroupVO itemGroupVO = new ItemGroupVO();
            itemGroupVO.setName(group.getAttrGroupName());

            //查询规格参数及值
            List<AttrAttrgroupRelationEntity> relationEntities = this.relationDao.selectList(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_group_id", group.getAttrGroupId()));
            List<Long> attrIds = relationEntities.stream().map(AttrAttrgroupRelationEntity::getAttrId).collect(Collectors.toList());
            ////.in("attr_id", attrIds)
            List<ProductAttrValueEntity> productAttrValueEntities = this.attrValueDao.selectList(new QueryWrapper<ProductAttrValueEntity>().eq("spu_id", spuId).eq("attr_id", attrIds));
            itemGroupVO.setBaseAttrs(productAttrValueEntities);

            return itemGroupVO;
        }).collect(Collectors.toList());

    }


    public static void main(String[] args) {

        CompletableFuture.supplyAsync(() -> {
            System.out.println("runAsync.......");
            return "hello supplyAsync";
        }).thenApply(t -> {
            System.out.println(t);
            return "heelo thenApply111";
        }).thenApply(t -> {
            System.out.println(t);
            return "heelo thenApply222";
        }).thenAccept(t -> {
            System.out.println(t);
        }).whenComplete((t, u) -> {
            System.out.println("t:  " + t);
            System.out.println(u);
        }).handleAsync((t, u) -> {
            System.out.println("t:  " + t);
            System.out.println(u);
            return "hello handleAsync";
        }).thenCombine(CompletableFuture.completedFuture("hello completedFuture"), (t, u) -> {
            System.out.println("asdasd" + t);
            System.out.println("asd" + u);
            return "thenCombine";
        });
    }

}